// pages/topic/publish/publish.js
var util = require('../../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-12-25',
    index: 0,
    picker: ['运动', '音乐', '庆祝', "学习", "吐槽", "创意", "运动会女","运动会男"],
  },
  PickerChange(e) {
    console.log("----PickerChange----")
    console.log(e.detail.value);
    this.setData({
      index: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var TIME = util.formatTime(new Date());
    this.setData({
      date: TIME,
    });
  },
  formSubmit: function (e) {
    var that = this
    console.log(e.detail.value)
    if (e.detail.value.title.length == 0) {
      wx.showModal({
        content: '标题不得为空！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.choice1.length == 0) {
      wx.showModal({
        content: '正方不得为空！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } else if (e.detail.value.choice2.length == 0) {
      wx.showModal({
        content: '反方不得为空！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return
    } 
    else {
      
      wx.request({
        url: app.globalData.url + 'topic', // 仅为示例，并非真实的接口地址
        data: {
          title: e.detail.value.title,
          choice1: e.detail.value.choice1,
          choice2: e.detail.value.choice2,
          user_id:app.globalData.user_id,
          end_time: that.data.date,
          type:that.data.index

        },
        method: "POST",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          if (res.data.state == 1) {
            wx.showToast({
              title: "上传成功", //这里打印出登录成功
              icon: 'success',
              duration: 1000
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/topic/topic',
              })
            }, 1000)
          } else {
            wx.showToast({
              title: "未知错误",
              icon: 'none',
              duration: 1000
            })


          }
        },
        fail(){

        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})